<?php
/**
 * Redis.class.php
 * Redis生成类
 * 
 * @author  王中艺
 * @date    2016-02-16
 */

namespace Common\Util;

class Redis extends \Redis{
    
    private static $_sharedInstance;

    static function getInstance() {
        if (!self::$_sharedInstance) {
            self::$_sharedInstance = new self();
        }

        return self::$_sharedInstance;
    }

    function __construct($host = null, $port = null, $pass = null){
        if($host == null)   $host   = C('REDIS_HOST');
        if($port == null)   $port   = C('REDIS_PORT');
        if($pass == null)   $pass   = C('REDIS_PASS');
        
        $this->connect($host, $port);
        if($pass)
            $this->auth($pass);
        
        //file_put_contents('./Public/redis.log',"redis\r\n", FILE_APPEND);
    }

    function cache($key, $value = null, $expire = null){
        if($value == null){
            return unserialize($this->get($key));
        }else{
            if($expire != null){
                $this->set($key, serialize($value), $expire);
            }else{
                $this->set($key, serialize($value));
            }
        }
    }
    
    function __destruct(){
        $this->close();
    }
}